<?php

namespace App\Models\Article;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;

class ArticleStatisticsDayModel extends BaseModel
{
    use HasFactory;
    use KfUid;
    use KfSoftDeletes;

    protected $table = "article_statistics_day";

    protected $fillable = [
        'id',
        'dateAt',
        'articleId',
        'source',
        'cumulateNum',
        'newNum',
        'cancelNum',
        'netIncNum',
        'isDeleted',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];

    const TYPE_LIKE    = 1;
    const TYPE_COLLECT = 2;
    const TYPE_SHARE   = 3;
    const TYPE_READ    = 4;
    const TYPE_SHOW    = 5;
    const TYPE_ARTICLE = 6;

    const TYPE_TEXT = [
        self::TYPE_LIKE    => "点赞",
        self::TYPE_COLLECT => "收藏",
        self::TYPE_SHARE   => "分享转发",
        self::TYPE_READ    => "阅读",
        self::TYPE_SHOW    => "展示",
        self::TYPE_ARTICLE => "内容",//总文章数
    ];

    /**
     * @return HasOne
     */
    public function articleRel(): HasOne
    {
        return $this->hasOne(ArticleModel::class, 'id', 'articleId');
    }

    /**
     * 批量插入数据
     * @param array $data
     * @param int $count
     */
    public static function batchInsertData(array $data, $count = 500)
    {
        if (count($data) > $count) {
            foreach (array_chunk($data, $count) as $chunk) {
                self::query()->insert($chunk);
            }
        } else {
            self::query()->insert($data);
        }
    }


}
